package com.young.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.young.common.execption.ServiceException;
import com.young.manager.mapper.CompanyDataBaseMapper;
import com.young.manager.model.CompanyDataBase;
import com.young.manager.service.CompanyDataBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Date;

@Slf4j
@RequiredArgsConstructor
@Service
public class CompanyDataBaseServiceImpl implements CompanyDataBaseService {

    private final CompanyDataBaseMapper companyDataBaseMapper;

    @Override
    public Page<CompanyDataBase> queryPage(Long companyId, String name, Integer pageNum, Integer pageSize) {
        Page<CompanyDataBase> page = Page.of(pageNum, pageSize);
        return companyDataBaseMapper.queryCompanyDBPage(page, companyId, name);
    }

    @Override
    public void insertCompanyDataBase(CompanyDataBase dataBase) {
        QueryWrapper<CompanyDataBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", dataBase.getCompanyId());
        if (companyDataBaseMapper.exists(queryWrapper)) throw  new ServiceException("数据源已配置！");
        dataBase.setCreateTime(new Date());
        dataBase.setModifyTime(dataBase.getCreateTime());
        companyDataBaseMapper.insert(dataBase);
    }

    @Override
    public void updateCompanyDataBase(CompanyDataBase dataBase) {
        companyDataBaseMapper.updateById(dataBase);
    }

    @Override
    public void deleteCompanyDataBase(Long id) {
        companyDataBaseMapper.deleteById(id);
    }
}
